package com.hong.aspect;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.util.StopWatch;

/**
 * <br>TODO(描述该类的作用)</br>
 *
 * @author 533735
 * @version 1.0
 * @email wanghong3@dafycredit.com
 * @date 2018/11/6 17:43
 * @since 1.0
 */
@Aspect
public class PerformanceTraceAspect {
    @Pointcut("execution(public void *.method1())")
    public void method1(){}

    @Pointcut("execution(public void *.method2())")
    public void method2(){}

    @Pointcut("method1() || method2()")
    public void compositePointcut(){}

    @Around("compositePointcut()")
    public Object performanceTrace(ProceedingJoinPoint joinPoint){
        StopWatch watch = new StopWatch();
        watch.start();
        Object obj = null;
        try {
            obj = joinPoint.proceed();
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        } finally {
            watch.stop();
            System.out.println("PT in method [" + joinPoint.getSignature().getName() + "]>>>" + watch.toString());
        }
        return obj;
    }
}
